package com.example.lib.utils.am

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Process
import com.orhanobut.logger.Logger
import java.util.*
import kotlin.system.exitProcess

object ActManger {

    // Activity栈
    val activityStack = Stack<Activity>()

    /**
     * 添加Activity到堆栈
     */
    fun addActivity(activity: Activity) {
        activityStack.add(activity)
    }

    /**
     * 添加Activity移出栈
     */
    fun removeActivity(activity: Activity) {
        activityStack.remove(activity)
    }

    /**
     * 获取当前Activity（堆栈中最后一个压入的）
     */
    fun currentActivity(): Activity? {
        return if (activityStack.isEmpty()) null
        else activityStack.lastElement()
    }

    /**
     * 获取当前Activity（堆栈中最后一个压入的）
     */
    fun currentActivityMaybeNull(): Activity? {
        return if (activityStack.isEmpty()) null
        else activityStack.lastElement()
    }

    /**
     * 结束指定的Activity
     */
    fun finishActivity(activity: Activity) {
        activityStack.remove(activity)
        activity.finish()
    }

    /**
     * 结束指定类名的Activity
     */
    fun finishActivity(cls: Class<*>) {
        for (activity in activityStack) {
            if (activity.javaClass == cls) {
                finishActivity(activity)
                return
            }
        }
    }

    /**
     * 是否有指定类名的Activity
     */
    fun hasActivity(cls: Class<*>): Boolean {
        return activityStack.map { it.javaClass }.filter { it == cls }.isNullOrEmpty().not()
    }

    /**
     * 是否有指定类名的Activity
     */
    fun hasActivity(cls: String): Boolean {
        return activityStack.map { it.javaClass.name }.filter { it == cls }.isNullOrEmpty().not()
    }

    /**
     * 结束所有Activity
     */
    fun finishAllActivity() {
        activityStack.forEach {
            it.finish()
        }
        activityStack.clear()
    }

    /**
     * 结束所有Activity
     */
    fun <T> finishOtherActivity(activity: Class<T>) {
        for (i in activityStack.indices) {
            val nowAct = activityStack[i]
            if (nowAct.javaClass.name != activity.name) {
                nowAct.finish()
                activityStack.remove(nowAct)
            }
        }
    }

    fun isNullOrEmpty() = activityStack.isNullOrEmpty()

    fun size() = if (isNullOrEmpty()) 0 else activityStack.size

    fun print() {
        activityStack.forEach {
            Logger.e("剩余Activity：${it::class.java.name}")
        }
    }

    /**
     * 退出应用程序
     */
    fun appExit() {
        finishAllActivity()
        Process.killProcess(Process.myPid())
        exitProcess(0)
    }

    //重启App
    fun restartApp(context: Context) {
        context.packageManager.getLaunchIntentForPackage(context.packageName)?.apply {
            addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
            context.startActivity(this)
        }
        appExit()
    }

    fun getTopActivity(): Activity? {
        if (activityStack.size > 0) {
            return activityStack.get(activityStack.size - 1)
        }
        return null
    }
}


